package com.live.util;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class StringUtil {

	/**
	 * check string if is null or empty
	 * 
	 * @param strs
	 * @return
	 */
	public static boolean isNullOrEmpty(String... strs) {
		boolean flag = false;
		
		for(String s : strs) {
			if(s == null || "".equals(s.trim())) {
				flag = true;
				break;
			}
		}
		
		return flag; 
	}
	
	/**
	 * combine a list data to a String connect by comma ","
	 * 
	 * @param datas
	 * @return
	 */
	public static String combineByComma(List<String> datas, boolean spaceSeparate) {
		
		if(datas == null) {
			return "";
		}
		
		StringBuffer sb = new StringBuffer();
		
		for(String o : datas) {
			
			if(sb.length() != 0) {
				sb.append("," + (spaceSeparate ? " " : ""));
			}
			
			sb.append(o);
		}
		
		return sb.toString();
	}
	
	/**
	 * ========================  text filter  ========================
	 */
	
	public static String filterHtmlTag(String text) {
		
		return text.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\n", "<br>").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
	}
	
	/**
	 * android : 
	 * mac os : iphone ipad
	 * windows phone:Nokia
	 */
    public static boolean isMobileDevice(String requestHeader){
		String[] deviceArray = new String[]{"android", "iphone", "windows phone","ipad", "mqqbrowser" };
		if(requestHeader == null)
			return false;
		requestHeader = requestHeader.toLowerCase();
		for(int i=0;i<deviceArray.length;i++){
			if(requestHeader.indexOf(deviceArray[i])>-1){
				return true;
			}
		}
		return false;
    }
    /**
     * get the datetime string
     * @return
     */
    public static String getStrDateTime() {
    	Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	return format.format(new Date());
    }
    
    public static String getStrDate() {
    	Format format = new SimpleDateFormat("yyyy-MM-dd");
    	return format.format(new Date());
    }
    
    public static String getPreDayDate(int reduceDay) {
    	Date dnow = new Date();
    	Date dPre = new Date();
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(dnow);
    	calendar.add(Calendar.DAY_OF_MONTH, -reduceDay);
    	dPre = calendar.getTime();
    	
    	Format format = new SimpleDateFormat("yyyy-MM-dd");
    	return format.format(dPre);
    }
    public static boolean isEmpty(String data){
    	if (null == data || "".equals(data.trim())||"null".equals(data.trim())) {
			return true;
		}
    	return false;
    }
    
}
